import {
	ApiSubmitOrder,
	ApiOrderList,
	ApiOrderDetail,
	ApiItemsListByOrderId
} from '@/common/request/api/order.js'
import {
	ORDERLIST
} from "../types.js"

const state = {
	loadingStatus:'loading',
	orderList:{
		records:[],
		data:[],
		current:0,
		pages:0,
		size:10,
		total:0
	},
}

const actions = {
	async ActOrderList({commit},payload){
		const res = await ApiOrderList(payload)
		if(res.success){
			commit('ORDERLIST',res.data)
		}
	},
	async ActOrderDetail({commit},payload){
		const res = await ApiOrderDetail(payload)
		return res
	},
	async ActItemsListByOrderId({commit},payload){
		const res = await ApiItemsListByOrderId(payload)
		return res
	},
	// 提交订单
	async ActSubmitOrder({commit},payload){
		uni.showLoading({
			mask:true,
			title:'正在提交'
		})
		const res = await ApiSubmitOrder(payload)
		uni.hideLoading()
		if(res.success){
			uni.removeStorageSync('order')
			uni.redirectTo({
				url:`/pages/order/orderConfirm?orderSn=${res.data.orderSn}&orderAmount=${res.data.orderAmount}&orderId=${res.data.orderId}&paymentId=${res.data.paymentId}`
			})
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
		return res
	},
}

const mutations = {
	[ORDERLIST](state,data){
		if(data.current === 1){
			data.data = data.records
		}else{
			data.data = state.orderList.data.concat(data.records)
		}
		if(data.current*data.size<=data.total){
			state.loadingStatus = 'loadmore'
		}else{
			state.loadingStatus = "nomore"
		}
		state.orderList = data
	},
}

const getters = {}

export default {
	state,
	mutations,
	actions,
	getters
}

